﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using papercut2.Data.Global;

namespace papercut2.Data.Fold
{
    class fold
    {
        //.......some info of the picturebox.........
        private static int initX = 0;//画板参考点（画板为可移动）
        private static int initY = 0;//画板参考点（画板为可移动）
        private static int boxsizeX = 0, boxsizeY = 0, currX = 0, currY = 0;
        private static Group picobject = new Group();
        private static bool flagVH = true;//
        private static bool flagLI = true;//
        public fold(int x, int y, int sizeX, int sizeY, LinkedList<bpoint> PstartList)
        {
            initX = x;
            initY = y;
            boxsizeX = currX = sizeX;
            boxsizeY = currY = sizeY;
            picobject.loadpicture(PstartList);

        }

        //...........................................
        public enum foldtype { horizon, vertical, tl_br, tr_bl } ;

        private static LinkedList<foldtype> mylist = new LinkedList<foldtype>();

        public static void hFold()
        {
            //........水平折叠..........................
            if (!flagVH) return;
            currY = currY / 2;
            mylist.AddLast(new LinkedListNode<foldtype>(foldtype.horizon));
            if (!flagLI)
            {
                flagVH = false;
                flagLI = true;
            }
        }
        public static void vFold()
        {
            if (!flagVH) return;
            currX = currX / 2;
            mylist.AddLast(new LinkedListNode<foldtype>(foldtype.vertical));
            if (!flagLI)
            {
                flagVH = false;
                flagLI = true;
            }
        }
        public static void tl_brFold()
        {
            if (currX != currY || !flagLI)
            {
                //..........not allowed to fold..
                return;
            }

            mylist.AddLast(new LinkedListNode<foldtype>(foldtype.tl_br));
            if (!flagVH)
            {
                flagLI = false;
                flagVH = true;
            }
        }
        public static void tr_blFold()
        {

            if (currX != currY || !flagLI)
            {
                //..........not allowed to fold..
                return;
            }
            mylist.AddLast(new LinkedListNode<foldtype>(foldtype.tr_bl));

            if (!flagVH)
            {
                flagLI = false;
                flagVH = true;
            }
        }
        public static void refolded()
        {
            for (int i = 0; i < mylist.Count; i++)
            {
                createimage(mylist.Last.Value);//........copy the image...........
                if (mylist.Count != 0)
                    mylist.RemoveLast();
            }


        }
        public static void createimage(foldtype temp)
        {
            LinkedList<bpoint> tpoint = picobject.getPstartList();
            double desX = 0, desY = 0;
            switch (temp)
            {
                case foldtype.horizon:
                    {
                        //.....refold the horizon fold............. 
                        desY = 2 * tpoint.First.Value.Y - currY;//最终位置
                        //.....................................
                        currX = currX * 2;
                        break;
                    }
                case foldtype.vertical:
                    {
                        //........refold the vertical fold.........
                        desX = 2 * tpoint.First.Value.X - currX;
                        currY = currY * 2;
                        break;
                    }
                case foldtype.tl_br:
                    {
                        //.........refold the topleft-bottomright
                        desX = tpoint.First.Value.Y;
                        desY = tpoint.First.Value.X;//XY坐标互换
                        break;
                    }
                case foldtype.tr_bl:
                    {
                        //..........refold the topright-bottomleft
                        desX = currX - tpoint.First.Value.Y;
                        desY = currX - tpoint.First.Value.X;
                        break;
                    }
                default:
                    {
                        break;

                    }
            }
            //.......use the desX and desY to copy new img........

            LinkedListNode<bpoint> tstart = tpoint.First;
            // new LinkedList...........
            LinkedList<bpoint> copypic = new LinkedList<bpoint>();

            while (tstart != null)
            {
                bpoint t = tstart.Value;
                double tempX, tempY;
                bpoint tnewpoint = null;
                while (t != null)
                {
                    tempX = desX - t.X;
                    tempY = desY - t.Y;
                    bpoint picpoint = new bpoint(tempX, tempY, 0, 0, null);
                    tnewpoint = picpoint;
                    tnewpoint = tnewpoint.Next;
                    t = t.Next;
                }
                tstart = tstart.Next;
                LinkedListNode<bpoint> tblistnode = new LinkedListNode<bpoint>(tnewpoint);
                copypic.AddLast(tblistnode);
            }
        }
        //........get and set.............
        public static void setPstartList(LinkedList<bpoint> item)
        {
            picobject.loadpicture(item);
        }
    }
}
